#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <algorithm>
using namespace std;

#define INF_MAX 0x3f3f3f;
typedef long long LL;
LL n;
int getMaxPrime(int x)
{
    int mx = 0;
    for (int i = 2; i <= x / i; i++)
    {
        if (x % i == 0)
        {
            while (x % i == 0)
            {
                x /= i;
            }
            mx = max(mx, i);
        }
    }
    if (x > 1)
    {
        mx = max(mx, x);
    }
    return mx;
}
int main()
{
    cin >> n;
    LL firstprime = getMaxPrime(n);
    if (n == firstprime)
    {
        cout << -1 << endl;
        return 0;
    }
    int mn = INF_MAX;
    for (int j = n - firstprime + 1; j <= n; j++)
    {
        int secondprime = getMaxPrime(j);
        if (secondprime == j)
        {
            continue;
        }
        mn = min(mn, j - secondprime + 1);
    }
    cout << mn << endl;
    return 0;
}